package com.wandian.texas.db.pool;

import java.io.FileInputStream;
import java.io.InputStream;
import java.sql.Connection;
import java.util.Properties;

import com.wandian.util.DESUtil;
import com.wandian.util.Log;
import com.wandian.util.MD5Security;

/**
 * @author long
 * @Create 2012-10-8 上午10:00:41
 * @QQ 108232706
 * @version 1.0
 * @description DBPoolMgr
 */
public class DBPoolMgr {

	public static String WANDIAN_POOL_NAME = "mysql";
	public static String LOG_POOL_NAME = "log";
	private static IDBPool wandianPool;
	private static IDBPool logPool;

	public static boolean init(String dbPath)
	{
		InputStream fileinputstream = null;
		try {
			fileinputstream = new FileInputStream(dbPath);
		} catch (Exception e) {
			String msg = String.format("未找到DB配置文件%s", new Object[] { dbPath });
			Log.error(msg, e);
		}
		Properties dbProps = new Properties();
		try {
			dbProps.load(fileinputstream);
		} catch (Exception e) {
			String msg = String.format("打开文件%s出错", new Object[] { dbPath });
			Log.error(msg, e);
			return false;
		}

		if (dbProps.getProperty(WANDIAN_POOL_NAME + ".url") != null) {
			wandianPool = createPools(WANDIAN_POOL_NAME, dbProps);
			if (wandianPool == null) {
				return false;
			}
		}

		// if (dbProps.getProperty(LOG_POOL_NAME + ".url") != null) {
		// logPool = createPools(LOG_POOL_NAME, dbProps);
		// if (logPool == null) {
		// return false;
		// }
		// }

		return true;
	}

	private static IDBPool createPools(String poolName, Properties props)
	{
		String url = props.getProperty(poolName + ".url");
		String user = props.getProperty(poolName + ".user");
		String password = props.getProperty(poolName + ".password");
		String maxconn = props.getProperty(poolName + ".maxconn", "5");
		String tempFallow = props.getProperty(poolName + ".fallow", "1");
		if (url == null)
			return null;
		int max;
		try {
			max = Integer.valueOf(maxconn).intValue();
		} catch (NumberFormatException e)
		{
			Log.error(String.format("[DataBase] 提示消息:%s; 错误描述:%s", new Object[] { "配置文件中最大连接数配置错误", "" }), e);
			max = 0;
		}
		int fallow;
		try {
			fallow = Integer.valueOf(tempFallow).intValue();
		} catch (Exception e)
		{
			fallow = 1;
		}
		try {
			IDBPool pool = new BoneCPDBPool(poolName, url, DESUtil.decrypt(user), DESUtil.decrypt(password), max, fallow);
			String msg = String.format("加载配置连接池:%s,URL:%s完成！", new Object[] { poolName, url });
			Log.info(msg);
			return pool;
		} catch (Exception e) {
			Log.error("创建db连接池失败 poolName : " + poolName, e);
		}
		return null;
	}

	public static void closePools()
	{
		if (wandianPool != null) {
			wandianPool.shutdown();
			wandianPool = null;
		}

		if (logPool != null) {
			logPool.shutdown();
			logPool = null;
		}
		Log.info("数据库连接池关闭成功");
	}

	public static Connection getwandianConn() {
		Connection conn = wandianPool.getConnection();
		return conn;
	}

	public static Connection getLogConn() {
		Connection conn = logPool.getConnection();
		return conn;
	}

	public static String getwandianPoolState() {
		if (wandianPool != null) {
			return wandianPool.getState();
		}
		return null;
	}

	public static String getLogPoolState() {
		if (logPool != null) {
			return logPool.getState();
		}
		return null;
	}

	public static String md5(String str) {
		if (str == null)
			return "";
		try {
			return MD5Security.compute(str);
		} catch (Exception e) {
			Log.error("MD5加密错误", e);
		}
		return "";
	}
}